# 离线 命令行版本查询数据
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import sys
import time
import readline
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities # get直接返回，不再等待界面加载完成
import os
import numpy as np

os.system('') # 解决win10无法显示转移字码颜色


# ANSI 转义码定义
RESET = "\033[0m"      # 重置所有属性
# 前景色
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"


side_eng_to_cn = {'':'', 'PLA':'中国(PLA)', 'PLANMC':'中国海军陆战队(PLANMC)', 'USA':'美国(USA)',
                  'USMC':'美国海军陆战队(USMC)', 'RUS':'俄罗斯(RUS)', 'VDV':'俄罗斯空降兵(VDV)',
                  'GB':'英国(GB)', 'CAF':'加拿大(CAF)', 'AUS':'澳大利亚(AUS)', 'INS':'老乡(INS)',
                  'MEA':'中东联盟(MEA)', 'IMF':'民兵(IMF)'}

# 因为html元素的id不是两个词，所以如果地图是两个词的话，所以要转换一下
map_dict = {'Al Barsrah':'AlBasrah', 'Fool\'s Road':'FoolsRoad', 'Goose Bay':'GooseBay', 'Kamdesh Highlands':'Kamdesh',
            'Kohat Toi':'Kohat', 'Lashkar Valley':'Lashkar', 'Logar Valley':'Logar', 'Sumari Bala':'Sumari',
            'Tallil Outskirts':'Tallil', 'Black Coast':'BlackCoast'
            }

# 处理两个单词地图的情况(比如：Talil Outskirts)
def parse_input(c : list):
    map1, mode1, version1, map2, mode2, version2 = '', '', '', '', '', ''

    if len(c) == 6: # 正常情况，两张地图都是一个词
        map1, mode1, version1, map2, mode2, version2 = c[0], c[1], c[2], c[3], c[4], c[5]
    elif len(c) == 7: # 一张地图两个单词，一张地图一个单词
        if c[1] not in  ['RAAS', 'AAS', 'Invasion', 'Seed', 'Tanks', 'Seed', 'TC']: # 第一张地图两个单词
            map1, mode1, version1, map2, mode2, version2 = map_dict[c[0] + ' ' + c[1]], c[2], c[3], c[4], c[5], c[6]
        else: # 第二张地图两个单词
            map1, mode1, version1, map2, mode2, version2 = c[0], c[1], c[2], map_dict[c[3] + ' ' + c[4]], c[5], c[6]
    else: # 两张地图都是两个词
        assert len(c) == 8
        map1, mode1, version1, map2, mode2, version2 = map_dict[c[0] + ' ' + c[1]], c[2], c[3], map_dict[c[4] + ' ' + c[5]], c[6], c[7]

    return map1, mode1, version1, map2, mode2, version2

# 设置自动补全
def completer(text, state):
    options = ['Anvil', 'Al Barsrah', 'Belaya', 'Chora', 'Fallujah',
               'Fool\'s Road', 'Goose Bay', 'Gorodok', 'Kamdesh Highlands',
               'Kohat Toi', 'Kokan', 'Lashkar Valley', 'Logar Valley', 'Manicouagan',
               'Mestia', 'Mutaha', 'Narva', 'Skorpo', 'Sumari Bala', 'Tallil Outskirts',
               'Yehorivka', 'Black Coast', 'Harju', 'RAAS', 'AAS', 'Invasion',
               'Seed', 'Tanks', 'Seed', 'TC']
    # 对每次输入的最后一个单词做匹配，然后自动补齐
    matches = [option for option in options if option.lower().startswith(text.split()[-1].lower())]
    if state < len(matches):
        return matches[state]
    else:
        return None

readline.set_completer(completer)
readline.parse_and_bind('tab: complete')

# 更新本场比赛地图
def update_now_map(map_arg, mode_arg, version_arg):
    version = int(version_arg[1:])
    special_zero = ''  # 因为他妈的如果地图大于10，就会变成0几，所以要特判某些地图
    if mode_arg == 'RAAS' and version < 10:  # 只有RAAS会地图大于10，其他模式目前还没大于10的
        if map_arg == 'Gorodok' or map_arg == 'Kohat' or  map_arg == 'Yehorivka':
            special_zero = '0'

    file_path = './data/' + map_arg + '/' + mode_arg + ' v' + special_zero + str(version) + '/car_info.npy'
    dict = np.load(file_path, allow_pickle=True).item()
    team1, team2 = list(dict.keys())[0], list(dict.keys())[1]
    return team1, team2


# 查询你所在下场比赛地图阵营
def query_next_map(map_arg, mode_arg, version_arg, side_arg):
    version = int(version_arg[1:])
    special_zero = ''  # 因为他妈的如果地图大于10，就会变成0几，所以要特判某些地图
    if mode_arg == 'RAAS' and version < 10:  # 只有RAAS会地图大于10，其他模式目前还没大于10的
        if map_arg == 'Gorodok' or map_arg == 'Kohat' or  map_arg == 'Yehorivka':
            special_zero = '0'

    file_path = './data/' + map_arg + '/' + mode_arg + ' v' + special_zero + str(version) + '/car_info.npy'

    dict = np.load(file_path, allow_pickle=True).item()
    team1, team2 = list(dict.keys())[0], list(dict.keys())[1]

    print('---------------')
    if side_arg == 1: # 第一队
        print(MAGENTA + '下一场你是', GREEN + team1 + RESET, MAGENTA + '阵营' + RESET)
        for value in dict[team1]:
            print(GREEN + value + RESET)
        print(MAGENTA + '敌方是', RED + team2 + RESET, MAGENTA + '阵营' + RESET)
        for value in dict[team2]:
            print(RED + value + RESET)

    else: # 第二队
        assert side_arg == 2 # 第二队
        print(MAGENTA + '下一场你是', GREEN + team2 + RESET, MAGENTA + '阵营' + RESET)
        for value in dict[team2]:
            print(GREEN + value + RESET)
        print(MAGENTA + '敌方是', RED + team1 + RESET, MAGENTA + '阵营' + RESET)
        for value in dict[team1]:
            print(RED + value + RESET)




while True:
    print('输入本场比赛信息和下场比赛信息:')
    a = input('> ')
    # a = 'Yehorivka RAAS v11 Gorodok RAAS v7'
    # b = 'yehorivka raas v11 gordok raas v7'
    c = a.split()

    map1_arg, model1_arg, version1_arg, map2_arg, model2_arg, version2_arg = parse_input(c)

    team1, team2 = update_now_map(map1_arg, model1_arg, version1_arg)
    # print(team1, team2)
    while True: # 若输错，重新输
        print('你本场比赛是 ' + side_eng_to_cn[team1] +' 还是 ' + side_eng_to_cn[team2] + ' 阵营?输入括号里的英文缩写')
        now_side = input()
        next_side = -1 # 下场比赛是哪边阵营
        if now_side == team1:
            next_side = 2
            break
        elif now_side == team2:
            next_side = 1
            break
        else:
            print(RED + '阵营输错啦' + RESET)
            # assert(False)

    query_next_map(map2_arg, model2_arg, version2_arg, next_side)
